package demo1;

import java.util.Arrays;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
//class Student implements Comparable<Student>{
//    public int age;
//    public String name;
//
//    public Student(int age, String name) {
//        this.age = age;
//        this.name = name;
//    }
//    @Override
//    public int compareTo(Student o) {
//        return this.age-o.age;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "age=" + age +
//                ", name='" + name + '\'' +
//                '}';
//    }
//
////    @Override
////    public int compareTo(Student o) {
////        return this.name.compareTo(o.name);
////    }
//}
//
//    public class Text {
//    public static void mySort(Comparable[] s){
//        for (int i = 0; i < s.length-1; i++) {
//            for (int j = 0; j < s.length-1-i; j++) {
//                if(s[j].compareTo(s[j+1])>0){
//                    Comparable tmp =s[j];
//                    s[j]=s[j+1];
//                    s[j+1]=tmp;
//                }
//            }
//        }
//    }
    class Student {
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
//    @Override
//    public int compareTo(Student o) {
//        return this.age-o.age;
//    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//        return this.name.compareTo(o.name);
//    }
}

    public class Text {
        public static void main(String[] args) {
            Student[] students=new Student[3];
            students[0]  = new Student(10, "zhangsang");
            students[1]  = new Student(18, "liashi");
            students[2]  = new Student(14,"aa");
            AgeComparator ageComparator=new AgeComparator();
            NameComparator nameComparator=new NameComparator();
            System.out.println("根据年龄排");
            System.out.println("排序前："+Arrays.toString(students));
            Arrays.sort(students,ageComparator);
            System.out.println("排序后："+Arrays.toString(students));
            System.out.println("根据名字排");
            System.out.println("排序前："+Arrays.toString(students));
            Arrays.sort(students,nameComparator);
            System.out.println("排序后："+Arrays.toString(students));
        }

        public static void main1(String[] args) {          //比较俩对象大小
            Student student1 = new Student(10, "zhangsang");
            Student student2 = new Student(14, "liashi");
            System.out.println(student1);
 //           System.out.println(student1.compareTo(student2));
//            System.out.println(student1.name.compareTo(student2.name));
        }
    }
